package com.weibo.favorite.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.weibo.common.BaseSeverlet;
import com.weibo.common.CommonUtil;
import com.weibo.favorite.Favorite;
import com.weibo.favorite.Service.FavoriteService;
import com.weibo.favorite.Service.imp.FavoriteServiceImp;
import com.weibo.sql.CommonParm;
import com.weibo.user.User;

public class FavoriteServlet extends BaseSeverlet {
	private static Logger logger =  Logger.getLogger(FavoriteServlet.class);
	@Override
	public void doService(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		//Object user_id = request.getAttribute("host_id");
		Object host_id = request.getParameter("host_id");
		if(host_id!=null&&!"".equals(host_id)){
			FavoriteService favoriteService = new FavoriteServiceImp();
			Favorite favorite = new Favorite();
			User current_user = CommonUtil.getLoginUser(request);
			try {
				request.setCharacterEncoding("UTF-8");
				//response.setContentType("text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				PrintWriter out = response.getWriter();
				if(current_user!=null){ 
					favorite.setHost_id((String)host_id);
					favorite.setUser_id(current_user.getUser_id());
					out.print(favoriteService.addFavorite(favorite));
				}else{
					out.print(CommonParm.LOGIN_TIMEOUT);
				}
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				logger.error(e);
			}
		}
	}

}
